package fastwave.cloud.demo.fastwave.biz.subscriber.services;

import fastwave.cloud.demo.fastwave.biz.subscriber.common.CacheUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <p>Title: EmailReceiver</p>
 * <p>Description: </p>
 *
 * @author: dwLiu
 * @date: 2020/4/7 0:01
 */
@Component
public class EmailReceiver {
    @Autowired
    CacheUtil cacheUtil;

//    @RabbitListener(queues = "EmailQueue")
//    public void receiver(String msg) throws Exception{
//        System.out.println("收到的消息："+msg);
////        throw new Exception("未成功消费消息");
//    }

    @RabbitListener(queues = "EmailQueue")
    public void receiver(Message message) throws Exception {
        String messageId = message.getMessageProperties().getMessageId();
        if (messageId != null && !cacheUtil.exists(messageId)) {
            String msg = new String(message.getBody());
            System.out.println("开始发送邮件" + msg);
            cacheUtil.set(messageId, true);
        } else {
            System.out.println("已经消费过了");
        }
    }
}
